﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Assessment.Management.SharedComponent.Params.CommonParam;
using Assessment.Management.SharedComponent.Constants;
using Assessment.Management.SharedComponent.Entities;

namespace Assessment.Management.DAO.CommonManagers
{
    public class OptionDAO : BaseDaos
    {
        public OptionDAO()
            : base()
        {
        }

        public void GetListOptionByQuestionID(OptionParam param)
        {
            string query = "Select * from Options where Deleted=@Deleted and QuestionID=@QuestionID";

            var cmd = base.GetSqlCommand();
            cmd.CommandText = query;
            cmd.Parameters.AddWithValue("@Deleted", SMX.smx_IsNotDeleted);
            cmd.Parameters.AddWithValue("@QuestionID", param.Option.QuestionID);
            try
            {
                param.listOption = base.ExecuteQueryGetListObject<Option>(cmd);
            }
            finally
            {
                base.CloseConnection(cmd);
            }
        }

        public void GetListHitCountByQuestionIDSubjectIDClassID(OptionParam param)
        {
            string query = @"Select o.ID, o.OPText, oh.HitCount from Options as o INNER JOIN OptionHit as oh ON o.ID = oh.OptionID
                                        where o.Deleted=@Deleted and o.QuestionID=@QuestionID and
                                              oh.ClassID = @ClassID and oh.SubjectID=@SubjectID";
            var cmd = base.GetSqlCommand();
            cmd.CommandText = query;
            cmd.Parameters.AddWithValue("@Deleted", SMX.smx_IsNotDeleted);
            cmd.Parameters.AddWithValue("@QuestionID", param.Option.QuestionID);
            cmd.Parameters.AddWithValue("@ClassID", param.Option.ClassID);
            cmd.Parameters.AddWithValue("@SubjectID", param.Option.SubjectID);
            try
            {
                param.listOption = base.ExecuteQueryGetListObject<Option>(cmd);
            }
            finally
            {
                base.CloseConnection(cmd);
            }
        }

        public void GetCountOptionByQuestionID(OptionParam param)
        {
            string query = "Select count(*) from Options where Deleted=@Deleted and QuestionID=@QuestionID";

            var cmd = base.GetSqlCommand();
            cmd.CommandText = query;
            cmd.Parameters.AddWithValue("@Deleted", SMX.smx_IsNotDeleted);
            cmd.Parameters.AddWithValue("@QuestionID", param.Option.QuestionID);
            try
            {
                param.Option.Count = base.ExecuteScalar(cmd);
            }
            finally
            {
                base.CloseConnection(cmd);
            }
        }
    }
}
